package com.admin.web;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.admin.bean.Menu;
import com.admin.service.MenuService;

/**
 * @author Jonsy
 *
 */
@Controller
@RequestMapping("/menu")
public class MenuController {

	@Autowired
	protected MenuService menuService;

	@RequestMapping(method = RequestMethod.POST, value = "/add")
	public String create(Menu menu) {
		menuService.create(menu);
		return "redirect:/menu";
	}

	@RequestMapping(value = "/{id}/modify", method = RequestMethod.POST)
	public String modify(@PathVariable("id") String id, Menu menu) {
		menu.setId(id);
		menuService.modify(menu);
		return "redirect:/menu";
	}

	@RequestMapping(value = "/{id}/status", method = RequestMethod.PUT)
	@ResponseBody
	public void switchStatus(@PathVariable("id") String id, @RequestParam("disable") boolean disable) {
		menuService.switchStatus(id, disable);
	}

	@RequestMapping(value = "/{id}/delete", method = RequestMethod.DELETE)
	@ResponseBody
	public void delete(@PathVariable("id") String id) {
		menuService.delete(id);
	}

	@RequestMapping(value = "/form", method = RequestMethod.GET)
	public String toform(@RequestParam(value = "id", required = false) String id,
			@RequestParam(value = "parent", required = false) boolean parent, Model model) {
		String url = null;
		if (StringUtils.isNotBlank(id) && !parent) {
			model.addAttribute("menu", menuService.get(id));
			url = "/menu/" + id + "/modify";
		} else {
			url = "/menu/add";
			if (StringUtils.isNotBlank(id)) {
				model.addAttribute("parentPath", id);
			}
		}
		model.addAttribute("api", url);
		return "menu/form";
	}

	@RequestMapping(method = RequestMethod.GET)
	public String list(Model model) {
		model.addAttribute("list", menuService.list());
		return "menu/list";
	}

}
